package com.fuchuang.seckillsystem.timer;

import com.fuchuang.seckillsystem.mapper.UserMapper;
import com.fuchuang.seckillsystem.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class UserTimer {
    
    @Autowired
    private IUserService userService;
    
    @Resource
    private UserMapper userMapper;
    
    /**
     * 将用户数据提前加载到redis中，防止秒杀时间段大量用户登录
     * 每天凌晨1点执行一次
     */
    @Scheduled(cron = "0 0 1 * * ? ")
    public void loadUserPushRedis() {
        //获取所有的用户名称
        List<String> allUsername = userMapper.getAllUsername();
        allUsername.forEach(username -> {
            userService.findUserDetailsByUsername(username);
        });
    }
}
